﻿Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.Runtime.Serialization
Imports System.ComponentModel
Imports ATS.Business.Entities.EntityStateClass


<DataContract(IsReference:=True)>
<KnownType(GetType(MainAssetsTable))>
Partial Public Class Category
    Implements INotifyPropertyChanged, IObjectWithChangeTracker, IEditableObject

    Private _isDirty As Boolean

#Region "Load Events"



    Private categoryData As Category

    Sub BeginEdit() Implements IEditableObject.BeginEdit
        categoryData = New Category
        categoryData._CategoryName = CategoryName
        categoryData._CategoryDesc = CategoryDesc
        categoryData._IsActive = IsActive
        categoryData._DepreciationMethod = DepreciationMethod
        categoryData._Years = Years
        categoryData._AssetClass = AssetClass
    End Sub


    Sub CancelEdit() Implements IEditableObject.CancelEdit

        CategoryName = categoryData._CategoryName
        CategoryDesc = categoryData._CategoryDesc
        IsActive = categoryData._IsActive
        DepreciationMethod = categoryData._DepreciationMethod
        Years = categoryData._Years
        AssetClass = categoryData._AssetClass

    End Sub


    Sub EndEdit() Implements IEditableObject.EndEdit
        categoryData = Nothing
    End Sub





    Public Sub New()
    End Sub

    Public Sub New(CategoryId As Integer, _
                   CategoryName As String, _
                   CategoryCode As String, _
                   CategoryDesc As String, _
                   DepreciationMethod As Nullable(Of Integer), _
                   Years As Nullable(Of Integer), _
                   AssetClass As String,
                   IsActive As Boolean)
        _IsActive = IsActive
        _CategoryId = CategoryId
        _CategoryName = CategoryName
        _CategoryCode = CategoryCode
        _CategoryDesc = CategoryDesc
        _DepreciationMethod = DepreciationMethod
        _Years = Years
        _AssetClass = AssetClass
    End Sub
#End Region

#Region "Primitive Properties"

    <DataMember()>
    Public Overridable Property CategoryId As Integer


    Private _CategoryName As String
    <DataMember()>
    Public Property CategoryName() As String
        Get
            Return _CategoryName
        End Get

        Set(ByVal value As String)
            If Not value Is Nothing Then
                If value <> _CategoryName Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "CategoryName")
                End If
            Else
                Throw New Exception("Please provide value for Category name.")
            End If
            _CategoryName = value
            OnPropertyChanged("CategoryName")
        End Set
    End Property


    Private _IsActive As Boolean
    <DataMember()>
    Public Property IsActive() As Boolean
        Get
            Return _IsActive
        End Get

        Set(ByVal value As Boolean)

            If value <> _IsActive Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "IsActive")
            End If

            _IsActive = value
            OnPropertyChanged("IsActive")
        End Set
    End Property

    Private _CategoryCode As String
    <DataMember()>
    Public Property CategoryCode() As String
        Get
            Return _CategoryCode
        End Get

        Set(ByVal value As String)
            If Not value Is Nothing Then
                If value <> _CategoryCode Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "CategoryCode")
                End If
            Else
                Throw New Exception("Please provide value for Category Code.")
            End If
            _CategoryCode = value
            OnPropertyChanged("CategoryCode")
        End Set
    End Property

    Private _CategoryDesc As String
    <DataMember()>
    Public Property CategoryDesc() As String
        Get
            Return _CategoryDesc
        End Get

        Set(ByVal value As String)
            If value <> _CategoryDesc Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "CategoryDesc")
            End If
            _CategoryDesc = value
            OnPropertyChanged("CategoryDesc")
        End Set
    End Property



    Private _DepreciationMethod As Nullable(Of Integer)
    <DataMember()>
    Public Property DepreciationMethod() As Nullable(Of Integer)
        Get
            Return _DepreciationMethod
        End Get

        Set(ByVal value As Nullable(Of Integer))
            If Not value = 0 Then
                If value <> _DepreciationMethod Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "DepreciationMethod")
                End If
            Else
                Throw New Exception("Please select a Depreciation Method.")
            End If
            _DepreciationMethod = value
            OnPropertyChanged("DepreciationMethod")
        End Set
    End Property


    Private _Years As Nullable(Of Integer)
    <DataMember()>
    Public Property Years() As Nullable(Of Integer)
        Get
            Return _Years
        End Get

        Set(ByVal value As Nullable(Of Integer))
            If Not value Is Nothing Then
                If value <> _Years Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "Years")
                End If
            Else
                Throw New Exception("Please provide useful life in Years.")
            End If
            _Years = value
            OnPropertyChanged("Years")
        End Set
    End Property

    Private _AssetClass As String
    <DataMember()>
    Public Property AssetClass() As String
        Get
            Return _AssetClass
        End Get

        Set(ByVal value As String)
            If Not value Is Nothing Then
                If value <> _AssetClass Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "AssetClass")
                End If
            Else
                Throw New Exception("Please select a Class.")
            End If
            _AssetClass = value
            OnPropertyChanged("AssetClass")
        End Set
    End Property



    Public ReadOnly Property IsDirty() As Boolean
        Get
            Return Me.EntityState <> EntityStateEnum.Unchanged
        End Get
    End Property

#End Region
#Region "Navigation Properties"

    <DataMember()>
    Public Property MainAssetsTables() As TrackableCollection(Of MainAssetsTable)
        Get
            If _mainAssetsTables Is Nothing Then
                _mainAssetsTables = New TrackableCollection(Of MainAssetsTable)
                AddHandler _mainAssetsTables.CollectionChanged, AddressOf FixupMainAssetsTables
            End If
            Return _mainAssetsTables
        End Get
        Set(ByVal value As TrackableCollection(Of MainAssetsTable))
            If _mainAssetsTables IsNot value Then
                If ChangeTracker.ChangeTrackingEnabled Then
                    Throw New InvalidOperationException("Cannot set the FixupChangeTrackingCollection when ChangeTracking is enabled")
                End If
                If _mainAssetsTables IsNot Nothing Then
                    RemoveHandler _mainAssetsTables.CollectionChanged, AddressOf FixupMainAssetsTables
                End If
                _mainAssetsTables = value
                If _mainAssetsTables IsNot Nothing Then
                    AddHandler _mainAssetsTables.CollectionChanged, AddressOf FixupMainAssetsTables
                End If
                OnNavigationPropertyChanged("MainAssetsTables")
            End If
        End Set
    End Property

    Private _mainAssetsTables As TrackableCollection(Of MainAssetsTable)

#End Region
#Region "ChangeTracking"

    Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
        If ChangeTracker.State <> ObjectState.Added AndAlso ChangeTracker.State <> ObjectState.Deleted Then
            ChangeTracker.State = ObjectState.Modified
        End If
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Protected Overridable Sub OnNavigationPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private _changeTracker As ObjectChangeTracker

    <DataMember()>
    Public Property ChangeTracker() As ObjectChangeTracker Implements IObjectWithChangeTracker.ChangeTracker
        Get
            If _changeTracker Is Nothing Then
                _changeTracker = New ObjectChangeTracker()
                AddHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
            Return _changeTracker
        End Get
        Set(ByVal value As ObjectChangeTracker)
            If _changeTracker IsNot Nothing Then
                RemoveHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
            _changeTracker = value
            If _changeTracker IsNot Nothing Then
                AddHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
        End Set
    End Property

    Private Sub HandleObjectStateChanging(ByVal sender As Object, ByVal e As ObjectStateChangingEventArgs)
        If e.NewState = ObjectState.Deleted Then
            Me.ClearNavigationProperties()
        End If
    End Sub

    Private _isDeserializing As Boolean
    Protected Property IsDeserializing() As Boolean
        Get
            Return _isDeserializing
        End Get
        Private Set(ByVal value As Boolean)
            _isDeserializing = value
        End Set
    End Property

    <OnDeserializing()>
    Public Sub OnDeserializingMethod(ByVal context As StreamingContext)
        IsDeserializing = True
    End Sub

    <OnDeserialized()>
    Public Sub OnDeserializedMethod(ByVal context As StreamingContext)
        IsDeserializing = False
        ChangeTracker.ChangeTrackingEnabled = True
    End Sub

    Protected Overridable Sub ClearNavigationProperties()
        MainAssetsTables.Clear()
    End Sub

#End Region
#Region "Association Fixup"

    Private Sub FixupMainAssetsTables(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
        If IsDeserializing Then
            Return
        End If

        If e.NewItems IsNot Nothing Then
            For Each item As MainAssetsTable In e.NewItems
                item.Category = Me
                If ChangeTracker.ChangeTrackingEnabled Then
                    If Not item.ChangeTracker.ChangeTrackingEnabled Then
                        item.StartTracking()
                    End If
                    ChangeTracker.RecordAdditionToCollectionProperties("MainAssetsTables", item)
                End If
            Next
        End If

        If e.OldItems IsNot Nothing Then
            For Each item As MainAssetsTable In e.OldItems
                If item.Category Is Me Then
                    item.Category = Nothing
                End If
                If ChangeTracker.ChangeTrackingEnabled Then
                    ChangeTracker.RecordRemovalFromCollectionProperties("MainAssetsTables", item)
                End If
            Next
        End If
    End Sub

#End Region

#Region "Support Procedures"
    Public Sub ResetDirtyBit()
        Me.EntityState = EntityStateEnum.Unchanged
    End Sub

    'Public Sub OnPropertyChanged(ByVal propertyName As String)
    '    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    'End Sub

    'Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Private _EntityState As EntityStateEnum
    Public Property EntityState() As EntityStateEnum
        Get
            Return _EntityState
        End Get
        Private Set(ByVal value As EntityStateEnum)
            _EntityState = value
        End Set
    End Property

    Private Sub DataStateChanged(ByVal dataState As EntityStateEnum, ByVal propertyName As String)
        ' Raise the event
        If Not String.IsNullOrEmpty(propertyName) Then
            RaiseEvent PropertyChanged(Me, _
            New PropertyChangedEventArgs(propertyName))
        End If

        ' If the state is deleted, mark it as deleted
        If dataState = EntityStateEnum.Deleted Then
            Me.EntityState = dataState
        End If

        If Me.EntityState = _
                       EntityStateEnum.Unchanged Then
            Me.EntityState = dataState
        End If
    End Sub



#End Region

End Class
